#!/usr/bin/perl -w
#
# $OpenBSD: configure,v 1.1 2001/01/06 16:43:44 naddy Exp $

use strict;

my @themes = split " ", $ENV{THEMES} or die "$0: must define THEMES\n";
my @classics = split " ", $ENV{CLASSICS} or die "$0: must define CLASSICS\n";
my @languages = split " ", $ENV{LANGUAGES} or die "$0: must define LANGUAGES\n";
my @flavor = split " ", $ENV{FLAVOR} or die "$0: must define FLAVOR\n";
my (%theme, %classic, %lang);
my $action;

if ($ARGV[0] eq 'chk') {
    $action = 0;
} elsif ($ARGV[0] eq 'conf') {
    $action = 1;
} else {
    die "usage: $0 chk|conf\n";
}

my $wrksrc = $ENV{WRKSRC} or die "$0: must define WRKSRC\n" if $action;

foreach my $i (@themes) { $theme{lc $i} = $i };
foreach my $i (@classics) { $classic{lc $i} = 1 };
foreach my $i (@languages) { $lang{$i} = 1 };

chdir $wrksrc or die "$0: can't chdir to $wrksrc: $!\n" if $action;

if ($#flavor == 0) {
    &invalid_flavor if !$theme{$flavor[0]} || $classic{$flavor[0]};
    symlink "themes/$theme{$flavor[0]}", "default_theme" if $action;
} elsif ($#flavor == 1) {
    if ($classic{$flavor[0]} && $lang{$flavor[1]}) {
	;
    } elsif ($classic{$flavor[1]} && $lang{$flavor[0]}) {
	@flavor = ($flavor[1], $flavor[0]);
    } else {
	&invalid_flavor;
    }
    mkdir "default_theme" if $action;
    foreach my $i (qw(config date.xpm clock.xpm led.xpm hour.xpm
		      minute.xpm second.xpm beats.xpm)) {
	symlink "../themes/$theme{$flavor[0]}/$i","default_theme/$i" if $action;
    }
    foreach my $i (qw(month.xpm weekday.xpm)) {
	symlink "../languages/$flavor[1]/$i", "default_theme/$i" if $action;
    }
} else {
    &invalid_flavor;
}

sub invalid_flavor {
    my (@a, $i);

    print STDERR "$0: invalid FLAVOR: @flavor\n\n";
    print STDERR "Available flavors are:\n";
    @a = sort keys %theme;
    for ($i = 0; $i <= $#a; $i++) {
	print STDERR "\t" if $i % 4 == 0;
	print STDERR $a[$i];
	print STDERR " <lang>" if $classic{$a[$i]};
	print STDERR ($i % 4 == 3) ? "\n" : ", " if $i != $#a;
    }
    print STDERR "\nwhere <lang> is a choice from\n";
    @a = sort keys %lang;
    for ($i = 0; $i <= $#a; $i++) {
	print STDERR "\t" if $i % 6 == 0;
	print STDERR $a[$i];
	print STDERR ($i % 6 == 5) ? "\n" : ", " if $i != $#a;
    }
    print STDERR "\n";
    exit 2;
}
